#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
#include<vector>
#define x first
#define y second
using namespace std;
using pii = pair<int, int>;
const int N = 55;
vector<pii> v1, v2;
char g[N][N];
bool st[N][N];
int n, m;
int ans = 0x3f3f3f3f;
int dx[] = { 1,0,-1,0 }, dy[] = { 0,-1,0,1 };
void dfs(int x, int y, vector<pii>& v)
{
    if (g[x][y] != 'X') return;
    v.push_back({ x,y });
    g[x][y] = 'a';
    st[x][y] = true;
    for (int i = 0; i < 4; i++)
    {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || b < 0 || a >= n || b >= m || st[a][b]) continue;
        dfs(a, b, v);
    }
    st[x][y] = false;
}
void findx(int& x, int& y)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (g[i][j] == 'X')
            {
                x = i, y = j;
                return;
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> g[i];
    int x = 0, y = 0;
    findx(x, y);
    dfs(x, y, v1);
    findx(x, y);
    dfs(x, y, v2);
    for (auto e1 : v1)
    {
        for (auto e2 : v2)
        {
            int cnt = abs(e1.x - e2.x) + abs(e1.y - e2.y) - 1;
            ans = min(ans, cnt);
        }
    }
    cout << ans << endl;
    return 0;
}